import { Injectable } from '@angular/core';
import { Store } from '@ngxs/store';
import { Router } from '@angular/router';
import { DemoSelector } from '../demo';
import { DemoComponent } from '../../pages/demo/demo.component';
import { routes } from '../../app-routing.module';

@Injectable({
  providedIn: 'root',
})
export class RouterService {

  constructor(
      private store: Store,
      private router: Router,
  ) {
  }

  init() {
    this.store.selectSnapshot(DemoSelector.list).forEach(r => {
      (routes as any).splice(routes.length - 1, 0, {path: `demo/${r.code}`, component: DemoComponent});
    });
    this.router.resetConfig([
      ...routes,
    ]);
  }
}
